﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wUniAppPCL.IO;

namespace wUniAppPCL.MVVM
{
  public class Averages
  {
    public Averages()
    {
      collection = new ObservableCollection<ChartData>();
    }

    private IEnumerable<Person> items;
    public IEnumerable<Person> Items
    {
      get { return items; }
      set
      {
        if (items != value)
        {
          items = value;
          Recalculate();
        }
      }
    }


    ObservableCollection<ChartData> collection;
    public ObservableCollection<ChartData> Collection
    {
      get
      {
        return collection;
      }
    }
      

    private void Recalculate()
    {
      //int t = Environment.TickCount;
      CalculateAge();
      CalculateHeight();
      CalculateWeight();
      Collection.Clear();
      Collection.Add(new ChartData("Age", Age));
      Collection.Add(new ChartData("Height", Height));
      Collection.Add(new ChartData("Weight", Weight));
      //t = Environment.TickCount - t;
      //wLogger.ToLog(t.ToString());
    }
    public double Age { get; set; }
    public double Height { get; set; }
    public double Weight { get; set; }

    public void CalculateAge()
    {
      Age = items.Average(c => c.Age);
    }
    public void CalculateHeight()
    {
      Height = items.Average(c => c.Height);
    }
    public void CalculateWeight()
    {
      Weight = items.Average(c => c.Weight);
    }
  }

}
